package JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import DAO.GoodsDaoImpl;
import DBConn.DBConn;
import MySQL.Goods;
public class goodsUpdate extends JFrame{
	private static final long serialVersionUID = -866582087055034098L;
	JLabel e1,e2,e3,e4,msg;
	JTextField t1,t2,t3,t4;
	JButton b1,b2,reset;
	Connection conn;
	public goodsUpdate(){
		conn = DBConn.open();
		e1 = new JLabel("商品代码:");
		t1 = new JTextField(10);
		t2 = new JTextField(10);
		t3 = new JTextField(10);
		t4 = new JTextField(10);
		reset= new JButton("刷新");
		t1.setText(""); b1 = new JButton("查询");
		e2 = new JLabel("商品名称:");  t2.setText("");
		e3 = new JLabel("价格:");  t3.setText("");
		e4 = new JLabel("规格:");  t4.setText("");
		msg = new JLabel(""); b2 = new JButton("修改");
		
		//字体设置
		Font f = new Font("隶书",Font.BOLD,23);
		e1.setFont(f);t1.setFont(f);
		e2.setFont(f);t2.setFont(f);
		e3.setFont(f);t3.setFont(f);
		e4.setFont(f);t4.setFont(f);
		b2.setFont(f); b2.setForeground(Color.blue);
		msg.setFont(f); msg.setForeground(Color.lightGray);
		b1.setFont(f);b1.setForeground(Color.blue);
		reset.setFont(f);
		
		//位置
		e1.setBounds(30, 20,110,30); t1.setBounds(145, 20,200, 30); 
		b1.setBounds(150, 80,90, 40); reset.setBounds(265, 80, 90, 40);
		e2.setBounds(30, 140,110,30); t2.setBounds(145, 140,200, 30);
		e3.setBounds(50, 200,110,30); t3.setBounds(145, 200,200, 30);
		e4.setBounds(50, 260,110,30); t4.setBounds(145, 260,200, 30);
		b2.setBounds(150,320,90,40); msg.setBounds(140, 400,240,30); 
		
		this.add(e1);this.add(t1);
		this.add(b1);this.add(reset);
		this.add(e2);this.add(t2);
		this.add(e3);this.add(t3);
		this.add(e4);this.add(t4);
		this.add(msg);
		//按钮监听
		b1.addActionListener(new B1());
		reset.addActionListener(new reSet());
		b2.addActionListener(new B2());
		
		this.setBounds(440,190,395,480);
		setTitle("修改信息");
		this.setLayout(null); // 绝对布局
		setVisible(true);
	}
	//内部类：按id查询
	class B1 implements ActionListener{
		Goods g = new Goods(); //new很关键
		GoodsDaoImpl gdi = new GoodsDaoImpl();
		public void actionPerformed(ActionEvent e)
		{
			try{
				g.setId(t1.getText());
				g = gdi.getGoodsById(g.getId());
				t2.setText(g.getName());
				t3.setText(String.valueOf(g.getPrice()));
				t4.setText(g.getSize());
				add(b2);
			}catch (Exception e1) {
				msg.setText("不存在此商品！");
				t1.requestFocus();
			}finally{
				DBConn.close(conn);  //关闭数据库连接
			}
		}
	}
	//内部类:b2修改
	class B2 implements ActionListener
	{
		Goods g = new Goods(); //new很关键
		GoodsDaoImpl gdi = new GoodsDaoImpl();
		public void actionPerformed(ActionEvent e)
		{
			g.setName(t2.getText());
			g.setPrice(Integer.parseInt(t3.getText()));
			g.setSize(t4.getText());
			g.setId(t1.getText());
			gdi.update(g);
			msg.setText("修改成功！");
		}
	}
	class reSet implements ActionListener{
		Goods g = new Goods(); //new很关键
		GoodsDaoImpl gdi = new GoodsDaoImpl();
		public void actionPerformed(ActionEvent e)
		{
			t1.setText("");
			t2.setText("");
			t3.setText("");
			t4.setText("");
			msg.setText("");
		}
	}
}
